// pages/infoedit/infoedit.js
import {reqCustomerInfo} from '../../api/profile'
import {reqCustomerAvatarUpload, reqCustomerInfoEdit} from '../../api/infoedit'
import { toast } from '../../utils/extendApi'


const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl:defaultAvatarUrl,
    nickname:''

  },

  async onchooseavatar(e){
    const { avatarUrl } = e.detail
    const res = await reqCustomerAvatarUpload(avatarUrl,'file')
    
    console.log(res)

    this.setData({
      avatarUrl:res.data
    })
  },

  onsaveinfo(){
    this.saveinfo()

    
  },

  async saveinfo(){
    const param ={
      customerAvatar:this.data.avatarUrl,
      customerName:this.data.nickname
    }

    const {code} = await reqCustomerInfoEdit(param)
    if (code===1){
      wx.toast({title:'修改成功',icon:'success'})
      wx.navigateBack()
    }else{
      wx.toast({title:'修改失败',icon:'error'})
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.loadinfo()
  },

  async loadinfo(){
    const {data} = await reqCustomerInfo()
    console.log(data)
      this.setData({
      avatarUrl:data.customerAvatar,
      nickname:data.customerName
    })
  }

  
})